package com.lanyou.cook.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.service.security.UserSessionService;

@Configuration
@EnableJpaAuditing
@EnableScheduling
public class ApplicationConfig {

	@Bean
	public AuditorAware<String> auditorProvider() {
		return new AuditorAware<String>() {
			@Override
			public String getCurrentAuditor() {
				String currentUserCode = UserSessionService.getCurrentAuthenticatedUserCode();
				if (currentUserCode != null) {
					return currentUserCode;
				} else {
					return Constants.JPA_ENTITY_DEFAULT_AUDITOR;
				}
			}
		};
	}
}
